<template>
    <div id="d1">
        <!--搜索框-->
        <el-row>
            <el-col :span="12">
                <el-form :inline="true" class="demo-form-inline">
                    <el-form-item label="评估标题">
                        <el-input v-model="propoTitle" placeholder="评估标题"></el-input>
                    </el-form-item>
                </el-form>
            </el-col>
            <el-col :span="5">
              <el-button type="primary" @click="search">查询</el-button>
            </el-col>
        </el-row>
        <!--表格-->
        <el-table :data="tableData" style="width: 100%">
            <el-table-column fixed prop="propoId" label="编号" width="150"></el-table-column>
            <el-table-column fixed prop="propoTitle" label="评估标题" width="300"></el-table-column>
            <el-table-column prop="propoContent" label="评估内容" width="500"></el-table-column>
            <el-table-column  fixed="right" label="操作" width="200">
                <template slot-scope="scope">
                    <el-button  type="text" @click="updatePropose(scope.row.propoId)">修改</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--分页控件-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[5, 10, 15, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
        <!--
            @size-change：页大小发生改变时触发
            @current-change：当前页发生改变时触发
            :current-page： 用于设置当前页
            :page-size：用于设置默认的页大小
            :total：用于设置总记录数
        -->
      <el-dialog title="修改评估建议方案" :visible.sync="dialogVisible">
        <el-form :model="propose" :rules="rules" ref="fromRules">
          <el-form-item label="id" :label-width="formLabelWidth" v-show="false">
            <el-input v-model="propose.propoId" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="方案标题" :label-width="formLabelWidth" prop="propoTitle" class="common">
            <el-input v-model="propose.propoTitle" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="评估建议描述" :label-width="formLabelWidth" prop="propoContent" class="common">
            <el-input v-model="propose.propoContent" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="upPropose('fromRules')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'propose',
  data () {
    return {
      dialogVisible: false,
      tableData: [],
      currentPage: 1, // 当前页
      pageSize: 5,
      total: 0,
      propoTitle: '',
      formLabelWidth: '120px',
      propose: {},
      rules: {
        propoTitle: [
          { required: true, message: '请输入方案标题', trigger: 'blur' }
        ],
        propoContent: [
          { required: true, message: '请输入评估建议描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    initBookData () {
      this.$axios.get(`/api/LljProposeServlet?pageNum=${this.currentPage}&pageSize=${this.pageSize}&propoTitle=${this.propoTitle}`).then(result => {
        this.tableData = result.data.rows
        this.total = result.data.total
      })
    },
    handleSizeChange (value) { // value是在页大小下拉框选择的值
      this.pageSize = value
      this.initBookData()
    },
    handleCurrentChange (value) { // value是当前页码
      this.currentPage = value
      this.initBookData()
    },
    search () {
      this.currentPage = 1
      this.initBookData()
    },
    updatePropose (id) {
      this.dialogVisible = true
      this.$axios.get(`/api/LljProposeServlet/byId?propoId=${id}`).then(result => {
        this.propose = result.data
        this.initBookData()
      })
    },
    upPropose (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('此操作将修改该食物, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$axios.put(`/api/LljProposeServlet`, this.propose).then(result => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.dialogVisible = false
              this.initBookData()
            })
          }).catch(() => {
            this.$message('已取消修改')
          })
        } else {
          return false
        }
      })
    }
  },
  created () {
    this.initBookData()
  }
}
</script>

<style>
</style>
